package tksDB

import (
	"github.com/astaxie/goredis"
)

import (
	. "tksStatic"
	"tksUint"
)

func ClearGatesInfo(redis *goredis.Client) {
	redis.Del("GateServerInfo")
}

func GetAviableGate(redis *goredis.Client) string {
	var m map[string]string = make(map[string]string)
	err := redis.Hgetall("GateServerInfo", m)
	if err != nil {
		return ""
	}
	var addr string = ""
	var val int = -1
	for k, v := range m {
		json, _ := tksUint.BufToMsg([]byte(v))
		i, _ := json.Get("ClientCount").Int()
		if i > val {
			addr = k
			val = i
		}
	}
	return addr
}

func UpdateGateInfo(redis *goredis.Client, gateInfo *GateServerInfo) {
	buf, _ := tksUint.MsgToBuf(gateInfo)
	redis.Hset("GateServerInfo", gateInfo.Addr, buf)
}
